<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	MYWIKINDX class.
*
*	Access My WIKINDX
*****/
class MYWIKINDX
{
// Constructor
	function MYWIKINDX($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/TABLE.php");
	}
// Check this is a logged in user accessing mywikindx
	function gatekeep()
	{
		if(!$this->session->getVar('setup_userId') || !array_key_exists('method', $this->vars))
			$this->failure($this->errors->text("inputError", "invalid"));
		$method = $this->vars['method'];
		if(array_key_exists('groupBib', $this->vars))
			$this->groupBib = TRUE;
		else
			$this->groupBib = FALSE;
		if($this->vars['method'] == 'createInit')
			$this->session->setVar('mywikindx_bibliography_radio', 'create');
		else if($this->vars['method'] == 'editInit')
			$this->session->setVar('mywikindx_bibliography_radio', 'edit');
		else if($this->vars['method'] == 'deleteInit')
			$this->session->setVar('mywikindx_bibliography_radio', 'delete');
		else if($this->vars['method'] == 'deleteFromInit')
			$this->session->setVar('mywikindx_bibliography_radio', 'deleteFrom');
		return $this->$method();
// else, proceed
	}
// List user's user groups with options to edit, delete and add new
	function listUserGroups()
	{
		$pString = FORM::formHeader("myWikindxUserGroup");
		$pString .= MISC::p(MISC::b($this->messages->text("user", "groups") . ":"));
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$recordset = $this->db->select(array('WKX_user_groups'), array('id', 'title'), 
			' WHERE ' . $this->db->formatField('adminId') . '=' . 
			$this->db->tidyInput($this->session->getVar('setup_userId')));
		if(!$this->db->numRows($recordset))
		{
			$pString .= TABLE::td($this->messages->text("user", "noGroups"));
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createGroupInit", TRUE) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createGroup"), FALSE, "left");
		}
		else
		{
			while($row = $this->db->loopRecordSet($recordset))
				$groups[$row['id']] = stripslashes($row['title']);
			asort($groups);
			$pString .= TABLE::td(FORM::selectFBoxValue(FALSE, "groupId", $groups, 5));
			if(!$this->session->getVar('mywikindx_group_radio'))
				$checked = TRUE;
			else
				$checked = $this->session->getVar('mywikindx_group_radio') == 'create' ? TRUE : FALSE;
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createGroupInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createGroup"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_group_radio') == 'edit' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "editGroupInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "editGroup"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_group_radio') == 'delete' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "deleteGroupInit", $checked) . 
				"&nbsp;&nbsp;" . $this->messages->text("user", "deleteGroup"), FALSE, "left");
		}
		$pString .= TABLE::td($radios);
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		return $pString;
	}
// List user's bibliographies with options to edit, delete and add new
	function listBibliographies()
	{
		$pString = FORM::formHeader("myWikindxBibliography");
		$pString .= MISC::p(MISC::b($this->messages->text("user", "bib") . ":"));
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if(!$this->session->getVar("mywikindx_bibliographies"))
		{
			$pString .= TABLE::td($this->messages->text("user", "noBibs"));
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createInit", TRUE) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createBib"), FALSE, "left");
		}
		else
		{
			$bibsArrayRaw = unserialize($this->session->getVar("mywikindx_bibliographies"));
			asort($bibsArrayRaw);
			foreach($bibsArrayRaw as $key => $value)
				$bibsArray[$key] = stripslashes($value);
			$pString .= TABLE::td(FORM::selectFBoxValue(FALSE, "bibId", $bibsArray, 5));
			if(!$this->session->getVar('mywikindx_bibliography_radio'))
				$checked = TRUE;
			else
				$checked = $this->session->getVar('mywikindx_bibliography_radio') == 'create' ? TRUE : FALSE;
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_bibliography_radio') == 'edit' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "editInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "editBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_bibliography_radio') == 'delete' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "deleteInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "deleteBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_bibliography_radio') == 'deleteFrom' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "deleteFromInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "deleteFromBib"), FALSE, "left");
		}
		$pString .= TABLE::td($radios);
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		return $pString;
	}
// List use group bibliographies of which user is ADMIN with options to edit, delete and add new
	function listGroupBibliographies()
	{
		$pString = FORM::formHeader("myWikindxGroupBibliography");
		$pString .= FORM::hidden("groupBib", TRUE);
		$pString .= MISC::p(MISC::b($this->messages->text("user", "groupBib") . ":"));
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if(!$this->session->getVar("mywikindx_groupbibliographies"))
		{
			$pString .= TABLE::td($this->messages->text("user", "noGroupBibs"));
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createInit", TRUE) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createBib"), FALSE, "left");
		}
		else
		{
			$bibsArrayRaw = unserialize($this->session->getVar("mywikindx_groupbibliographies"));
			asort($bibsArrayRaw);
			foreach($bibsArrayRaw as $key => $value)
				$bibsArray[$key] = stripslashes($value);
			$pString .= TABLE::td(FORM::selectFBoxValue(FALSE, "bibId", $bibsArray, 5));
			if(!$this->session->getVar('mywikindx_groupbibliography_radio'))
				$checked = TRUE;
			else
				$checked = $this->session->getVar('mywikindx_groupbibliography_radio') == 
				'create' ? TRUE : FALSE;
			$radios = MISC::p(FORM::radioButton(FALSE, "method", "createInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "createBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_groupbibliography_radio') == 'edit' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "editInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "editBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_groupbibliography_radio') == 'delete' ? TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "deleteInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "deleteBib"), FALSE, "left");
			$checked = $this->session->getVar('mywikindx_groupbibliography_radio') == 'deleteFrom' ? 
				TRUE : FALSE;
			$radios .= MISC::p(FORM::radioButton(FALSE, "method", "deleteFromInit", $checked) . "&nbsp;&nbsp;" . 
				$this->messages->text("user", "deleteFromBib"), FALSE, "left");
		}
		$pString .= TABLE::td($radios);
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		return $pString;
	}
// List user's bibliographies with options to use one of them or the WIKINDX master bibliography when listing, searching etc.
	function useBibInit()
	{
		$pString = FORM::formHeader("myWikindxBibliography");
		$pString .= FORM::hidden("method", "useBib");
		$pString .= MISC::p(MISC::b($this->messages->text("user", "useBib") . ":"));
// grab other users' bibliographies
		$otherBibs = $bibsU = $bibsUG = $bibsArray = array();
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('id', 'title', 'bibliography'), 
			" WHERE " . $this->db->formatfield('bibliography') . ' IS NOT NULL' . 
			' AND ' . $this->db->formatfield('userId') . '!=' . 
			$this->db->tidyInput($this->session->getVar('setup_userId')) . 
			' ORDER BY ' . $this->db->tidyInputClause('title'));
		while($row = $this->db->loopRecordSet($recordset))
		{
			if($row['bibliography'])
				$otherBibs[$row['id']] = $this->html->dbToFormTidy($row['title']);
		}
// Get this user's bibliographies
		if($this->session->getVar('mywikindx_bibliographies'))
		{
			$bibsRaw = unserialize($this->session->getVar("mywikindx_bibliographies"));
			foreach($bibsRaw as $key => $value)
				$bibsU[$key] = $this->html->dbToFormTidy($value);
		}
// Get this user's user group bibliographies
		if($this->session->getVar('mywikindx_groupbibliographies'))
		{
			$bibsRaw = unserialize($this->session->getVar("mywikindx_groupbibliographies"));
			foreach($bibsRaw as $key => $value)
				$bibsUG[$key] = $this->html->dbToFormTidy($value);
		}
// add main wikindx bibliography to array with id of 0
		$bibsArray[0] = $this->messages->text("user", "masterBib");
		if(!empty($bibsU))
		{
			$bibsArray[-1] = $this->messages->text('user', 'userBibs');
			foreach($bibsU as $key => $value)
				$bibsArray[$key] = $value;
		}
		if(!empty($bibsUG))
		{
			$bibsArray[-2] = $this->messages->text('user', 'userGroupBibs');
			foreach($bibsUG as $key => $value)
				$bibsArray[$key] = $value;
		}
		if(!empty($otherBibs))
		{
			$bibsArray[-3] = $this->messages->text('user', 'otherBibs');
			foreach($otherBibs as $key => $value)
				$bibsArray[$key] = $value;
		}
		$selected = $this->session->getVar('mywikindx_bibliography_use');
		if($selected)
			$pString .= FORM::selectedBoxValue(FALSE, "bibId", $bibsArray, $selected, 10);
		else
			$pString .= FORM::selectFBoxValue(FALSE, "bibId", $bibsArray, 10);
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		return $pString;
	}
// Set a bibliography for browsing
	function useBib()
	{
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
// bibId of 0 == master bibliography
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'])
			$this->session->delVar('mywikindx_bibliography_use');
		else if(array_key_exists('bibId', $this->vars) && ($this->vars['bibId'] < 0))
			$this->badInput($this->errors->text("inputError", "invalid"), "initDisplay", $user);
		else
			$this->session->setVar('mywikindx_bibliography_use', $this->vars['bibId']);
		$this->session->delVar('mywikindx_bibliography_add');
		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx"));
		$pString = $this->success->text("bibliography", " " . $this->messages->text("misc", "set") . " ");
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Display for deleting resources from a bibliography
	function deleteFromInit($message = FALSE)
	{
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUser("initDisplay");
		$pString = $message ? $message : '';
		$this->session->delVar('mywikindx_bibliography_add');
		$this->session->delVar('mywikindx_pagingStart');
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		$bibs = $this->db->fetchOne($recordset);
		if(!$bibs)
			$this->failure($this->messages->text("resources", "noResult"));
		include_once("core/user/LISTBIBLIOGRAPHY.php");
		$list = new LISTBIBLIOGRAPHY($this->db, $this->vars);
		list($string, $pageString) = $list->process($bibs, $this->groupBib);
		$pString .= $string;
		$this->template->setVar('paging', $pageString);
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "deleteFromBib")));
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Delete resources from a bibliography
	function deleteFrom()
	{
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUser("initDisplay");
		foreach($this->vars as $key => $value)
		{
			if(preg_match("/bib_(.*)/", $key, $matches))
				$inputBibs[] = $matches[1];
		}
		if(!isset($inputBibs))
			$this->failure($this->errors->text("inputError", "invalid"));
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
// bibliography is a comma-separated list of unique resource ids.
		$dbBibs = $this->db->fetchOne($recordset);
		$dbBibs = explode(",", $dbBibs);
		$dbBibs = array_diff($dbBibs, $inputBibs);
		if(empty($dbBibs))
			$this->db->updateNull('WKX_user_bibliography', array('bibliography'),
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		else
		{
			$updateArray = array('bibliography' => implode(",", $dbBibs));
			$this->db->update('WKX_user_bibliography', $updateArray, 
				" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		}
		$this->checkPopulatedBibliographies();
		$pString = $this->success->text("deleteFromBib");
		return $this->deleteFromInit($pString);
	}
// Display options for adding a user group
	function createGroupInit($error = FALSE)
	{
		$this->session->delVar('mywikindx_group_add');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "createGroup")));
		$this->session->delVar("mywikindx_groupLock");
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("myWikindxUserGroup");
		$pString .= FORM::hidden("method", "createGroup");
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$sessVar = $this->session->issetVar("mywikindx_groupTitle") ? 
			$this->html->dbToFormTidy($this->session->getVar("mywikindx_groupTitle")) : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("user", "groupTitle"), 
			"title", $sessVar, 50, 255) . " " . MISC::span('*', 'required'));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$sessVar = $this->session->issetVar("mywikindx_groupDescription") ? 
			$this->html->dbToFormTidy($this->session->getVar("mywikindx_groupDescription")) : FALSE;
		$pString .= TABLE::td(FORM::textAreaInput($this->messages->text("user", "groupDescription"), 
			"description", $sessVar, 80, 10) . MISC::br() . 
			$this->messages->text("hint", "format"));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		$users = $user->grabAll(TRUE);
// add 0 => IGNORE to $array
		$temp[0] = $this->messages->text("misc", "ignore");
		foreach($users as $key => $value)
		{
			if($key == $this->session->getVar('setup_userId'))
				continue;
			$temp[$key] = $value;
		}
		$pString .= MISC::p(FORM::selectFBoxValueMultiple($this->messages->text("user", "groupUserAdd"), 
			"users", $temp, 10) . MISC::br() . $this->messages->text("hint", "multiples"));
		$pString .= MISC::p(FORM::formSubmit("Add"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// create a user group
	function createGroup()
	{
		if(array_key_exists('description', $this->vars) && trim($this->vars['description']))
		{
			$this->session->setVar('mywikindx_groupDescription', $this->vars['description']);
			$fields[] = 'description';
			$values[] = $this->vars['description'];
		}
		if(!trim($this->vars['title']))
			$this->badInput($this->errors->text("inputError", "missing"), "createGroupInit");
		if($this->session->getVar("mywikindx_groupLock"))
			$this->failure($this->errors->text("done", "group"));
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "createGroup")));
		$this->session->setVar('mywikindx_groupTitle', $this->vars['title']);
		$userId = $this->session->getVar('setup_userId');
		$fields[] = 'title';
		$values[] = $this->vars['title'];
		$fields[] = 'adminId';
		$values[] = $userId;
		$userIds[] = $userId;
		foreach($this->vars['users'] as $userId)
		{
			if(!$userId) // IGNORE
				continue;
			$userIds[] = $userId;
		}
		$fields[] = 'userIds';
		$values[] = join(',', $userIds);
		$this->db->insert('WKX_user_groups', $fields, $values);
		$this->session->setVar("mywikindx_groupLock", TRUE);
		$this->session->delVar('mywikindx_groupDescription');
		$this->session->delVar('mywikindx_groupTitle');
		$pString = $this->success->text("group", " " . $this->messages->text("misc", "added") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Display options for adding a bibliography
	function createInit($error = FALSE)
	{
// If creating a group bibliography, this user must own groups
		if($this->groupBib)
		{
			$recordset = $this->db->select(array('WKX_user_groups'), 'title', 
				' WHERE ' . $this->db->formatField('adminId') . '=' . 
				$this->db->tidyInput($this->session->getVar('setup_userId')));
			if(!$this->db->numRows($recordset))
			{
				include_once("core/user/USER.php");
				$user = new USER($this->db, $this->vars);
				$this->badInput($this->errors->text("inputError", "userHasNoGroups"), "initDisplay", $user);
			}
		}
		$this->session->delVar('mywikindx_bibliography_add');
		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "createBib")));
		$this->session->delVar("bibliography_lock");
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("myWikindxBibliography");
		$pString .= FORM::hidden("method", "create");
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$sessVar = $this->session->issetVar("mywikindx_title") ? 
			$this->html->dbToFormTidy($this->session->getVar("mywikindx_title")) : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("user", "bibTitle"), 
			"title", $sessVar, 50, 255) . " " . MISC::span('*', 'required'));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$sessVar = $this->session->issetVar("mywikindx_description") ? 
			$this->html->dbToFormTidy($this->session->getVar("mywikindx_description")) : FALSE;
		$pString .= TABLE::td(FORM::textAreaInput($this->messages->text("user", "bibDescription"), 
			"description", $sessVar, 80, 10) . MISC::br() . 
			$this->messages->text("hint", "format"));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		if($this->groupBib)
		{
			$pString .= FORM::hidden('groupBib', TRUE);
			$recordset = $this->db->select(array('WKX_user_groups'), array('id', 'title'), 
				' WHERE ' . $this->db->formatField('adminId') . '=' . 
				$this->db->tidyInput($this->session->getVar('setup_userId')) . 
				' ORDER BY ' . $this->db->tidyInputClause('title'));
			while($row = $this->db->loopRecordSet($recordset))
				$groups[$row['id']] = $this->html->dbToFormTidy($row['title']);
			$pString .= MISC::p(FORM::selectFBoxValue($this->messages->text("user", "addGroupsToBib"), 
				"groupId", $groups, 10));
		}
		$pString .= MISC::p(FORM::formSubmit("Add"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// create a bibliography
	function create()
	{
		if(array_key_exists('description', $this->vars) && trim($this->vars['description']))
		{
			$this->session->setVar('mywikindx_description', $this->vars['description']);
			$fields[] = 'description';
			$values[] = $this->vars['description'];
		}
		if(!trim($this->vars['title']))
			$this->badInput($this->errors->text("inputError", "missing"), "createInit");
		if($this->session->getVar("bibliography_lock"))
			$this->failure($this->errors->text("done", "bibliography"));
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "createBib")));
		$this->session->setVar('mywikindx_title', $this->vars['title']);
		$fields[] = 'title';
		$values[] = $this->vars['title'];
		$fields[] = 'userId';
		$values[] = $this->session->getVar('setup_userId');
		if(array_key_exists('groupId', $this->vars) && $this->vars['groupId'])
		{
			$fields[] = 'userGroupId';
			$values[] = $this->vars['groupId'];
		}
		$this->db->insert('WKX_user_bibliography', $fields, $values);
		$bibDbId = $this->db->lastAutoId();
		if($this->groupBib)
		{
			if(array_key_exists('groupId', $this->vars) && $this->vars['groupId']) // valid id -- not 'IGNORE'
			{
				$recordset = $this->db->select(array('WKX_user_groups'), 'bibliographyIds',
					" WHERE " . $this->db->formatField('id') . "=" . 
					$this->db->tidyInput($this->vars['groupId']));
				$rowBibIds = $this->db->fetchOne($recordset);
				if(!$rowBibIds)
					$this->db->updateSingle('WKX_user_groups', 
						$this->db->formatField('bibliographyIds') .  "=" . $this->db->tidyInput($bibDbId),
						" WHERE " . $this->db->formatField('id') . "=" . 
						$this->db->tidyInput($this->vars['groupId']));
				else
				{
					$bibs = split(',', $rowBibIds);
					if(array_search($bibDbId, $bibs) === FALSE)
					{
						$bibs[] = $bibDbId;
						$this->db->updateSingle('WKX_user_groups', 
							$this->db->formatField('bibliographyIds') .  "=" . 
							$this->db->tidyInput(join(',', $bibs)),
							" WHERE " . $this->db->formatField('id') . "=" . 
							$this->db->tidyInput($this->vars['groupId']));
					}
				}
			}
		}
		$this->session->setVar("bibliography_lock", TRUE);
		$this->session->delVar('mywikindx_description');
		$this->session->delVar('mywikindx_title');
		$pString = $this->success->text("bibliography", " " . $this->messages->text("misc", "added") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Display options for editing a user group
	function editGroupInit($error = FALSE)
	{
		$this->session->delVar('mywikindx_group_add');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "editGroup")));
		$this->session->delVar("mywikindx_groupLock");
		if(!array_key_exists('groupId', $this->vars) || !trim($this->vars['groupId']))
			$this->failure($this->errors->text("inputError", "missing"));
		$this->checkValidUserGroup("initDisplay");
		$pString = '';
		$groupUserIds = $groupUsers = array();
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("myWikindxUserGroup");
		$pString .= FORM::hidden("method", "editGroup");
		$pString .= FORM::hidden("groupId", $this->vars['groupId']);
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if($this->session->issetVar("mywikindx_groupTitle"))
		{
			$title = $this->html->dbToFormTidy($this->session->getVar("mywikindx_groupTitle"));
			$description = $this->html->dbToFormTidy($this->session->getVar("mywikindx_groupDescription"));
		}
		else
		{
			$recordset = $this->db->select(array('WKX_user_groups'), 
				array('title', 'description', 'userIds', 'bibliographyIds'),
				' WHERE ' . $this->db->formatField('id') . '=' . 
				$this->db->tidyInput($this->vars['groupId']));
			$row = $this->db->fetchRow($recordset);
			$title = $this->html->dbToFormTidy($row['title']);
			$description = $this->html->dbToFormTidy($row['description']);
			if($row['userIds'])
				$groupUserIds = split(',', $row['userIds']);
		}
		if(!empty($groupUserIds))
		{
			include_once("core/html/HTML.php");
			$html = new HTML();
			foreach($groupUserIds as $id)
				$conditionArray[] = $this->db->formatField('id') . '=' . $this->db->tidyInput($id);
			$condition = " WHERE " . join(' OR ' , $conditionArray);
			$recordset = $this->db->select(array('WKX_users'), array('id', 'username', 'fullname', 'admin'), 
				$condition . ' ORDER BY ' . $this->db->tidyInputClause('username'));
			$groupUsers[0] = $this->messages->text("misc", "ignore");
			while($row = $this->db->loopRecordSet($recordset))
			{
				if($row['id'] == $this->session->getVar('setup_userId'))
					continue;
				$groupUsers[$row['id']] = $html->dbToFormTidy($row['username']);
				if($row['fullname'])
					$groupUsers[$row['id']] .= " (" . $html->dbToFormTidy($row['fullname']) . ")";
				if($row['admin'] == 'Y')
					$groupUsers[$row['id']] .= " ADMIN";
			}
		}
		$pString .= TABLE::td(FORM::textInput($this->messages->text("user", "groupTitle"), "title", 
			$title, 50, 255) . " " . MISC::span('*', 'required'));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$pString .= TABLE::td(FORM::textAreaInput($this->messages->text("user", "groupDescription"), 
			"description", $description, 80, 10) . MISC::br() . $this->messages->text("hint", "format"));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		$users = $user->grabAll(TRUE);
// add 0 => IGNORE to $array
		$temp[0] = $this->messages->text("misc", "ignore");
		foreach($users as $key => $value)
		{
			if($key == $this->session->getVar('setup_userId'))
				continue;
			if(array_key_exists($key, $groupUsers))
				continue;
			$temp[$key] = $value;
		}
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if(sizeof($groupUsers) > 1)
			$pString .= TABLE::td(FORM::selectFBoxValueMultiple($this->messages->text("user", 
			"groupUserDelete"), "groupUsers", $groupUsers, 10) . MISC::br() . 
			$this->messages->text("hint", "multiples"));
		if(sizeof($temp) > 1)
			$pString .= TABLE::td(FORM::selectFBoxValueMultiple($this->messages->text("user", "groupUserAdd"), 
				"users", $temp, 10) . MISC::br() . $this->messages->text("hint", "multiples"));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::p(FORM::formSubmit("Edit"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// edit a user group
	function editGroup()
	{
		if(trim($this->vars['description']))
		{
			$this->session->setVar('mywikindx_groupDescription', $this->vars['description']);
			$updateArray['description'] = $this->vars['description'];
		}
		else
			$this->db->updateNull('WKX_user_groups', array('description'),
				" WHERE " . $this->db->formatField('id') . "=" . 
				$this->db->tidyInput($this->vars['groupId']));
		if(!array_key_exists('groupId', $this->vars) || !$this->vars['groupId'] || !trim($this->vars['title']))
			$this->badInput($this->errors->text("inputError", "missing"), "editGroupInit");
		$recordset = $this->db->select(array('WKX_user_groups'), 'userIds',
			' WHERE ' . $this->db->formatField('id') . '=' . $this->db->tidyInput($this->vars['groupId']));
		$existingUsers = split(',', $this->db->fetchOne($recordset));
		if(array_key_exists('users', $this->vars))
		{
			foreach($this->vars['users'] as $id) // to be added
			{
				if(!$id) // IGNORE
					continue;
				if(array_search($id, $existingUsers) === FALSE)
					$existingUsers[] = $id;
			}
		}
		if(array_key_exists('groupUsers', $this->vars))
		{
			foreach($this->vars['groupUsers'] as $id) // to be deleted
			{
				if(!$id) // IGNORE
					continue;
				if(($deleteId = array_search($id, $existingUsers)) !== FALSE)
					unset($existingUsers[$deleteId]);
			}
		}
// This user's id is always in $existingUsers
		$updateArray['userIds'] = join(',', $existingUsers);
		$this->checkValidUserGroup("initDisplay");
		if($this->session->getVar("mywikindx_groupLock"))
			$this->failure($this->errors->text("done", "group"));
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "editGroup")));
		$updateArray['title'] = $this->vars['title'];
		$this->db->update('WKX_user_groups', $updateArray, 
			" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($this->vars['groupId']));
		$this->session->setVar("mywikindx_groupLock", TRUE);
		$this->session->delVar('mywikindx_groupDescription');
		$this->session->delVar('mywikindx_groupTitle');
		$pString = $this->success->text("group", " " . $this->messages->text("misc", "edited") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Display options for editing a bibliography
	function editInit($error = FALSE)
	{
		$this->session->delVar('mywikindx_bibliography_add');
		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "editBib")));
		$this->session->delVar("bibliography_lock");
		if(!array_key_exists('bibId', $this->vars) || !trim($this->vars['bibId']))
			$this->failure($this->errors->text("inputError", "missing"));
		$this->checkValidUser("initDisplay");
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("myWikindxBibliography");
		$pString .= FORM::hidden("method", "edit");
		$pString .= FORM::hidden("bibId", $this->vars['bibId']);
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if($this->session->issetVar("mywikindx_title"))
		{
			$title = $this->html->dbToFormTidy($this->session->getVar("mywikindx_title"));
			$description = $this->html->dbToFormTidy($this->session->getVar("mywikindx_description"));
		}
		else
		{
			if($this->groupBib)
				$bibsArray = unserialize($this->session->getVar("mywikindx_groupbibliographies"));
			else
				$bibsArray = unserialize($this->session->getVar("mywikindx_bibliographies"));
			$title = $this->html->dbToFormTidy($bibsArray[$this->vars['bibId']]);
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('description', 'userGroupId'), 
				" WHERE " . $this->db->formatfield('id') . "=" . 
				$this->db->tidyInput($this->vars['bibId']));
			$row = $this->db->fetchRow($recordset);
			$description = $this->html->dbToFormTidy($row['description']);
			$userGroupId = $row['userGroupId'];
		}
		$pString .= TABLE::td(FORM::textInput($this->messages->text("user", "bibTitle"), "title", 
			$title, 50, 255) . " " . MISC::span('*', 'required'));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$pString .= TABLE::td(FORM::textAreaInput($this->messages->text("user", "bibDescription"), 
			"description", $description, 80, 10) . MISC::br() . $this->messages->text("hint", "format"));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		if($userGroupId) // This is a user group bibliography
		{
			$pString .= FORM::hidden('groupBib', TRUE);
			$recordset = $this->db->select(array('WKX_user_groups'), array('id', 'title'), 
				' WHERE ' . $this->db->formatField('adminId') . '=' . 
				$this->db->tidyInput($this->session->getVar('setup_userId')) . 
				' ORDER BY ' . $this->db->tidyInputClause('title'));
			while($row = $this->db->loopRecordSet($recordset))
				$groups[$row['id']] = $this->html->dbToFormTidy($row['title']);
			$pString .= MISC::p(FORM::selectedBoxValue($this->messages->text("user", "addGroupsToBib"), 
				"groupId", $groups, $userGroupId, 10));
		}
		$pString .= MISC::p(FORM::formSubmit("Edit"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// edit a bibliography
	function edit()
	{
		if(trim($this->vars['description']))
		{
			$this->session->setVar('mywikindx_description', $this->vars['description']);
			$updateArray['description'] = $this->vars['description'];
		}
		else
			$this->db->updateNull('WKX_user_bibliography', array('description'),
				" WHERE " . $this->db->formatField('id') . "=" . 
				$this->db->tidyInput($this->vars['bibId']));
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'] || !trim($this->vars['title']))
			$this->badInput($this->errors->text("inputError", "missing"), "editInit");
		$this->checkValidUser("initDisplay");
		if($this->session->getVar("bibliography_lock"))
			$this->failure($this->errors->text("done", "bibliography"));
		$this->session->setVar('mywikindx_title', $this->vars['title']);
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "editBib")));
		$updateArray['title'] = $this->vars['title'];
		if(array_key_exists('groupId', $this->vars)) // user group bibliography
			$updateArray['userGroupId'] = $this->vars['groupId'];
		$this->db->update('WKX_user_bibliography', $updateArray, 
			" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		$this->session->setVar("bibliography_lock", TRUE);
		$this->session->delVar('mywikindx_description');
		$this->session->delVar('mywikindx_title');
		$pString = $this->success->text("bibliography", " " . $this->messages->text("misc", "edited") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Ask for confirmation of delete user group
	function deleteGroupInit()
	{
		if(!array_key_exists('groupId', $this->vars) || !$this->vars['groupId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUserGroup("initDisplay");
		$this->session->delVar('mywikindx_group_add');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "deleteGroup")));
		$recordset = $this->db->select(array('WKX_user_groups'), array('title'), 
			' WHERE ' . $this->db->formatField('id') . '=' . 
			$this->db->tidyInput($this->vars['groupId']));
		$title = $this->db->fetchOne($recordset);
		$pString = MISC::p(MISC::b($this->messages->text("user", "deleteConfirmGroup") . ": ") . 
			$this->html->dbToHtmlTidy($title));
		$pString .= MISC::p($this->messages->text("user", "deleteGroup2"));
		$pString .= FORM::formHeader("myWikindxUserGroup");
		$pString .= FORM::hidden("method", "deleteGroup");
		$pString .= FORM::hidden("groupId", $this->vars['groupId']);
		$pString .= MISC::br() . FORM::formSubmit('Confirm');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Ask for confirmation of delete bibliography
	function deleteInit()
	{
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUser("initDisplay");
		$this->session->delVar('mywikindx_bibliography_add');
		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "deleteBib")));
		if($this->groupBib)
			$bibs = unserialize($this->session->getVar("mywikindx_groupbibliographies"));
		else
			$bibs = unserialize($this->session->getVar("mywikindx_bibliographies"));
		$title = $bibs[$this->vars['bibId']];
		$pString = MISC::p(MISC::b($this->messages->text("user", "deleteConfirmBib") . ": ") . 
			$this->html->dbToHtmlTidy($title));
		$pString .= FORM::formHeader("myWikindxBibliography");
		if($this->groupBib)
			$pString .= FORM::hidden("groupBib", TRUE);
		$pString .= FORM::hidden("method", "delete");
		$pString .= FORM::hidden("bibId", $this->vars['bibId']);
		$pString .= MISC::br() . FORM::formSubmit('Confirm');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Delete user group
	function deleteGroup()
	{
		if(!array_key_exists('groupId', $this->vars) || !$this->vars['groupId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUserGroup("initDisplay");
// Get any bibliographyIds and delete those bibliographies
		$recordset = $this->db->select(array('WKX_user_groups'), array('bibliographyIds'), 
			' WHERE ' . $this->db->formatField('id') . '=' . 
			$this->db->tidyInput($this->vars['groupId']));
		$bibIds = $this->db->fetchOne($recordset);
		if($bibIds)
		{
			$bibs = split(',', $bibIds);
			foreach($bibs as $bibId)
				$this->db->delete('WKX_user_bibliography',
					" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($bibId));
			$this->db->optimize('WKX_user_bibliography');
		}
		$this->db->delete('WKX_user_groups',
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['groupId']));
		$this->db->optimize('WKX_user_groups');
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "deleteGroup")));
		$pString = $this->success->text("group", " " . $this->messages->text("misc", "deleted") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Delete bibliography
	function delete()
	{
		if(!array_key_exists('bibId', $this->vars) || !$this->vars['bibId'])
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->checkValidUser("initDisplay");
		$this->db->delete('WKX_user_bibliography',
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		$this->db->optimize('WKX_user_bibliography');
		if($this->vars['bibId'] == $this->session->getVar('mywikindx_bibliography_use'))
			$this->session->delVar('mywikindx_bibliography_use');
		$this->checkPopulatedBibliographies();
		if($this->groupBib) // remove bibliography id from all user groups
		{
			$recordset = $this->db->select(array('WKX_user_groups'), array('id', 'bibliographyIds'));
			while($row = $this->db->loopRecordSet($recordset))
			{
				if(!$row['bibliographyIds'])
					continue;
				$bibs = split(',', $row['bibliographyIds']);
				if(($bibId = array_search($this->vars['bibId'], $bibs)) !== FALSE)
				{
					unset($bibs[$bibId]);
					if(empty($bibs))
						$this->db->updateNull('WKX_user_groups', array('bibliographyIds'),
							" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($row['id']));
					else
					{
						$updateArray['bibliographyIds'] = join(',', $bibs);
						$this->db->update('WKX_user_groups', $updateArray, 
							" WHERE " . $this->db->formatField('id') . "=" . 
							$this->db->tidyInput($row['id']));
					}
				}
			}
		}
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx", 
			": " . $this->messages->text("user", "deleteBib")));
		$pString = $this->success->text("bibliography", " " . $this->messages->text("misc", "deleted") . " ");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// Add resources to a bibliography
	function add()
	{
		if(!array_key_exists('bibId', $this->vars) || ($this->vars['bibId'] < 1))
		{
			include_once("core/user/USER.php");
			$user = new USER($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $this->chooseDisplay($this->errors->text("inputError", "invalid")));
		}
// valid user?
		$bibs = array();
		if($this->session->getVar('mywikindx_bibliographies'))
			$bibs = unserialize($this->session->getVar("mywikindx_bibliographies"));
		if($this->session->getVar('mywikindx_groupbibliographies'))
			$bibs = array_merge($bibs, unserialize($this->session->getVar("mywikindx_groupbibliographies")));
// User group bibs of which this user is a member (therefore, has write access to)
		$bibsUG = array();
		$userId = $this->session->getVar('setup_userId');
		$recordset = $this->db->select(array('WKX_user_groups'), 
			array(array('WKX_user_bibliography.id' => 'bibId'), 
				array('WKX_user_bibliography.title' => 'bibTitle')), 
			"LEFT JOIN " . $this->db->formatTable('WKX_user_bibliography') . 
				" ON FIND_IN_SET(" . $this->db->formatField('WKX_user_bibliography.id') . ", CONCAT(" . 
				$this->db->formatfield('bibliographyIds') . ", ','))" . 
			" WHERE " . 
			" FIND_IN_SET(" . $this->db->tidyInput($userId) . ", CONCAT(" . 
				$this->db->formatfield('userIds') . ", ','))" . 
			' ORDER BY ' . $this->db->tidyInputClause('WKX_user_bibliography.title'));
		while($row = $this->db->loopRecordSet($recordset))
		{
			if($row['bibId'])
				$bibsUG[$row['bibId']] = $row['bibTitle'];
		}
		if(!array_key_exists(trim($this->vars['bibId']), $bibs) && 
			!array_key_exists(trim($this->vars['bibId']), $bibsUG))
		{
			include_once("core/user/USER.php");
			$user = new USER($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $this->chooseDisplay($this->errors->text("inputError", "invalid")));
		}
		if(!array_key_exists("selectAll", $this->vars))
		{
			foreach($this->vars as $key => $value)
			{
				if(preg_match("/bib_(.*)/", $key, $matches))
					$inputBibs[] = $matches[1];
			}
			if(!isset($inputBibs))
				return $this->chooseDisplay($this->errors->text("inputError", "invalid"));
		}
// else - want to add all in list to bibliography.
		else
		{
			if(!$sql = stripslashes($this->session->getVar('sql_stmt')))
				$this->failure($this->errors->text("file", "noSql"));
			$recordset = $this->db->query($sql);
			while($row = $this->db->loopRecordSet($recordset))
				$inputBibs[] = $row['resourceId'];
		}
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
// bibliography is a comma-separated list of unique resource ids.
		$dbBibsRow = $this->db->fetchOne($recordset);
		if(!$dbBibsRow)
			$updateArray = array('bibliography' => implode(",", $inputBibs));
		else
		{
			$dbBibs = explode(",", $dbBibsRow);
			$updateBibs = array_unique(array_merge($dbBibs, $inputBibs));
			$updateArray = array('bibliography' => implode(",", $updateBibs));
		}
		$this->db->update('WKX_user_bibliography', $updateArray, 
			" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($this->vars['bibId']));
		$pString = $this->success->text("addBib");
		$this->session->setVar('mywikindx_bibliography_add', $this->vars['bibId']);
		$this->session->setVar("setup_bibliographies", TRUE);
		return $this->chooseDisplay($pString);
	}
// Set email notification
	function notify()
	{
		if(!array_key_exists('notify', $this->vars) || !$this->vars['notify'])
			$this->failure($this->errors->text("inputError", "missing"));
		$updateArray = array('notify' => $this->vars['notify']);
		if(array_key_exists('notifyAdd', $this->vars) && array_key_exists('notifyEdit', $this->vars))
		{
			$this->session->setVar("mywikindx_notifyAdd", TRUE);
			$this->session->setVar("mywikindx_notifyEdit", TRUE);
			$updateArray['notifyAddEdit'] = 'A';
		}
		else if(array_key_exists('notifyAdd', $this->vars))
		{
			$this->session->setVar("mywikindx_notifyAdd", TRUE);
			$this->session->delVar("mywikindx_notifyEdit");
			$updateArray['notifyAddEdit'] = 'N';
		}
		else if(array_key_exists('notifyEdit', $this->vars))
		{
			$this->session->setVar("mywikindx_notifyEdit", TRUE);
			$this->session->delVar("mywikindx_notifyAdd");
			$updateArray['notifyAddEdit'] = 'E';
		}
		else
		{
			$this->session->setVar("mywikindx_notifyAdd", TRUE);
			$this->session->setVar("mywikindx_notifyEdit", TRUE);
			$updateArray['notifyAddEdit'] = 'A';
		}
		if(array_key_exists('notifyThreshold', $this->vars))
		{
			$this->session->setVar("mywikindx_notifyThreshold", $this->vars['notifyThreshold']);
			$updateArray['notifyThreshold'] = $this->vars['notifyThreshold'];
		}
		$this->db->update('WKX_users', $updateArray, 
			" WHERE " . $this->db->formatField('id') . "=" . 
			$this->db->tidyInput($this->session->getVar('setup_userId')));
		$this->session->setVar("mywikindx_notify", $this->vars['notify']);
		$pString = $this->success->text("notify");
		include_once("core/user/USER.php");
		$user = new USER($this->db, $this->vars);
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $user->initDisplay($pString));
	}
// When adding a resource from list, search, select resources, chose which one to return to.
// if sql_front is set in session, it overrides all (it's cleared in LISTCOMMON::listResources)
	function chooseDisplay($pString)
	{
		if($this->session->getVar('sql_front'))
		{
			include_once("core/front/FRONT.php");
			$front = new FRONT($this->db);
			$front->message = $pString;
			return $front->display();
		}
		if(array_search('lastMulti', $this->vars) === FALSE)
			$this->vars['lastMulti'] = TRUE;
		if($this->session->getVar('sql_lastMulti') == 'select')
		{
			include_once("core/list/SELECTRESOURCES.php");
			$select = new SELECTRESOURCES($this->db, $this->vars);
			return $select->process($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'statistics')
		{
			include_once("core/list/SELECTRESOURCES.php");
			$this->vars['statistics'] = $this->session->getVar('statistics_type');
			$this->vars['id'] = $this->session->getVar('statistics_id');
			$select = new SELECTRESOURCES($this->db, $this->vars);
			return $select->process();
		}
		else if($this->session->getVar('sql_lastMulti') == 'list')
		{
			include_once("core/list/LISTRESOURCES.php");
			$list = new LISTRESOURCES($this->db, $this->vars);
			return $list->process($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'search')
		{
			include_once("core/list/SEARCHRESOURCES.php");
			$search = new SEARCHRESOURCES($this->db, $this->vars);
			return $search->process($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'powerSearch')
		{
			include_once("core/list/POWERSEARCHRESOURCES.php");
			$search = new POWERSEARCHRESOURCES($this->db, $this->vars);
			return $search->process($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listCategory')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->categoryProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listCollection')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->collectionProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listPublisher')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->publisherProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listCreator')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->creatorProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listKeyword')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->keywordProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'listCite')
		{
			include_once("core/list/LISTSOMERESOURCES.php");
			$list = new LISTSOMERESOURCES($this->db, $this->vars);
			return $list->citeProcess($pString);
		}
		else if($this->session->getVar('sql_lastMulti') == 'basket')
		{
			include_once("core/basket/BASKET.php");
			$obj = new BASKET($this->db, $this->vars);
			return $obj->view($pString);
		}
	}
// check this user can edit, delete and deleteFrom for user bibliographies
	function checkValidUser($function)
	{
		if(!$this->groupBib)
			$bibs = unserialize($this->session->getVar('mywikindx_bibliographies'));
		else
			$bibs = unserialize($this->session->getVar('mywikindx_groupbibliographies'));
		if(!array_key_exists(trim($this->vars['bibId']), $bibs))
		{
			include_once("core/user/USER.php");
			$user = new USER($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $user->$function($this->errors->text("inputError", "invalid")));
		}
	}
// check this user can edit, delete and deleteFrom from user groups
	function checkValidUserGroup($function)
	{
		$recordset = $this->db->select(array('WKX_user_groups'), 'adminId', 
			' WHERE ' . $this->db->formatField('id') . '=' . 
			$this->db->tidyInput($this->vars['groupId']));
		$adminId = $this->db->fetchOne($recordset);
		if($this->session->getVar('setup_userId') != $adminId)
		{
			include_once("core/user/USER.php");
			$user = new USER($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $user->$function($this->errors->text("inputError", "invalid")));
		}
	}
// Check we still have populated user bibliographies
	function checkPopulatedBibliographies()
	{
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('id'), 
			" WHERE " . $this->db->formatField('bibliography') . ' IS NOT NULL');
		if($this->db->numRows($recordset))
			$this->session->setVar("setup_bibliographies", TRUE);
		else
			$this->session->delVar("setup_bibliographies");
	}
// Error handling
	function badInput($error, $function, $object = FALSE)
	{
		if(!$object)
			$object = $this;
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $object->$function($error));
	}
// failure - bomb out
	function failure($error)
	{
		$this->template->setVar("heading", $this->messages->text("heading", "myWikindx"));
		include_once("core/html/CLOSE.php");
		$this->template->setVar('body', $error);
		new CLOSE($this->db, $this->template->process());
	}
}
?>
